﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class User_PrivateConfiguration : System.Web.UI.Page
{
    UsersRepository URepo = new UsersRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            int userId = 0;
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] != null)
                {
                    userId = URepo.GetUserIdByName(_user["username"]);
                    var a = URepo.GetConfigurationByUserId(userId);
                    if (a != null)
                    {
                        chkToEmail.Checked = (a.ReceiveEmail != null) ? Convert.ToBoolean(a.ReceiveEmail) : false;
                        chkMsgAdmin.Checked = (a.ReceiveMessagefromAdmin != null) ? Convert.ToBoolean(a.ReceiveMessagefromAdmin) : false;
                        chkMsgUser.Checked = (a.ReceiveMessagefromUser != null) ? Convert.ToBoolean(a.ReceiveMessagefromUser) : false;
                        hdfConfig.Value = a.Id.ToString();
                    }
                }
            }
            else
                Response.Redirect(ResolveUrl("~/Index.html"));
        }
    }

    protected void btnSaveConfig_Click(object sender, EventArgs e)
    {
        try
        {
            int Id = (hdfConfig.Value != null)? int.Parse(hdfConfig.Value) : 0;
            bool status = false;
            if (Id > 0)
                status = URepo.UpdateConfiguration(Id, chkToEmail.Checked, chkMsgAdmin.Checked, chkMsgUser.Checked);
            if (status)
            {
                lblMsg.Text = "Cập nhật thành công";
                lblMsg.Icon = Icon.Accept;
                lblMsg.AddClass("success");
            }
            else
            {
                lblMsg.Text = "Cập nhật không thành công";
                lblMsg.Icon = Icon.Error;
                lblMsg.AddClass("failure");
            }
        }
        catch
        {
        }
    }
    protected void chkMsgAdmin_CheckedChanged(object sender, EventArgs e)
    {
        lblMsg.Text = (this.chkMsgAdmin.Checked) ? "True" : "False";
    }
}